<template>
    <d2-container>
        <!-- 搜索表头 -->
        <template slot="header">
            <el-form :inline="true" :model="searchForm" ref="searchForm" size="small" v-if="showHeader" class="searchHeader">

                <el-form-item label="分类状态" prop="categoryStatus">
                    <ec-dict-select v-model="searchForm.categoryStatus" placeholder="选择类型" dictType="enable_state">
                    </ec-dict-select>
                </el-form-item>

                <el-form-item label="分类编码" prop="categoryCode">
                    <el-input v-model="searchForm.categoryCode" placeholder="分类编码" clearable />
                </el-form-item>

                <el-form-item label="分类名称" prop="categoryName">
                    <el-input v-model="searchForm.categoryName" placeholder="分类名称" clearable />
                </el-form-item>

                <el-form-item label="操作">
                    <el-button type="primary" @click="getDataTable">
                        <d2-icon name="search" />
                        查询
                    </el-button>
                    <el-button @click="handleFormReset">
                        <d2-icon name="refresh" />
                        重置
                    </el-button>
                </el-form-item>

            </el-form>
        </template>

        <!-- 表格表头 -->
        <div class="table-header">
            <div>
                <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addOrUpdate()"
                    v-if="isAuth()">添加
                </el-button>
                <el-button type="primary" icon="el-icon-caret-top" size="small" v-if="!tigger"
                    @click="tableExpandTigger()">展开</el-button>
                <el-button type="primary" icon="el-icon-caret-bottom" size="small" v-else @click="tableExpandTigger()">
                    合并
                </el-button>
                <el-button type="danger" icon="el-icon-delete" size="small" @click="deleteHandle()"
                    :disabled="!multipleSelection.length >0" v-if="isAuth()">批量删除
                    <span v-if="multipleSelection.length >0">【{{multipleSelection.length}}条】</span>
                </el-button>
                <!-- <el-button type="primary" icon="el-icon-search" size="small">搜索</el-button> -->
                <!-- <el-button type="primary" icon="el-icon-search" size="small">搜索</el-button> -->
            </div>
            <div>
                <ec-prompt-button content="刷新" icon="el-icon-refresh" @click="getDataTable()" v-if="isAuth()"></ec-prompt-button>
                <ec-prompt-button content="搜索" icon="el-icon-search" @click="changeHeader()" v-if="isAuth()"></ec-prompt-button>
                <ec-prompt-button content="导入" icon="el-icon-download" v-if="isAuth()"></ec-prompt-button>
                <ec-prompt-button content="导出" icon="el-icon-upload2" v-if="isAuth()"></ec-prompt-button>
                <ec-prompt-button content="分享" icon="el-icon-share" v-if="isAuth()"></ec-prompt-button>
            </div>
        </div>


        <!-- 表格表体 -->
        <ec-table :data="tableData" @selection-change="handleSelectionChange" ref="dataTable" row-key="pkCategory" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column type="selection" header-align="center" align="center" width="50">
            </el-table-column>
            <!-- <el-table-column prop="pkCategory" header-align="center" align="center" show-overflow-tooltip label="分类主键">
            </el-table-column> -->
            <el-table-column prop="categoryCode" header-align="center" align="center" show-overflow-tooltip
                label="分类编码">
            </el-table-column>
            <el-table-column prop="categoryName" header-align="center" align="center" show-overflow-tooltip
                label="分类名称">
            </el-table-column>
            <el-table-column prop="categoryLevelDesc" header-align="center" align="center" show-overflow-tooltip
                label="分类级别">
            </el-table-column>
            <el-table-column prop="categoryIcon" header-align="center" align="center" show-overflow-tooltip
                label="分类图片">
            </el-table-column>
            <el-table-column prop="categorySort" header-align="center" align="center" show-overflow-tooltip
                label="分类排序">
            </el-table-column>
            <el-table-column prop="categoryStatusDesc" header-align="center" align="center" show-overflow-tooltip
                label="分类状态">
            </el-table-column>
            <!-- <el-table-column prop="pid" header-align="center" align="center" show-overflow-tooltip label="父级分类">
            </el-table-column> -->
            <el-table-column fixed="right" header-align="center" align="center" width="120" label="操作">
                <template slot-scope="scope">
                    <!-- <el-button type="text" size="small" @click="managerHandle(scope.row.pkCategory)"
                        class="el-icon-setting" v-if="isAuth()">管理
                    </el-button> -->
                    <el-button type="text" size="small" @click="addOrUpdate(scope.row.pkCategory)" class="el-icon-edit"
                        v-if="isAuth()">修改
                    </el-button>
                    <el-button type="text" size="small" @click="deleteHandle(scope.row.pkCategory)"
                        class="el-icon-delete" v-if="isAuth()">删除
                    </el-button>
                </template>
            </el-table-column>

        </ec-table>

        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataTable"></add-or-update>
    </d2-container>
</template>

<script>
    import * as goodcategoryService from '@/api/mall/goodcategory'
    import AddOrUpdate from './add-or-update'
    import PageTableMixin from '@/mixin/page-table'

    export default {
        name: 'cache',
        mixins: [PageTableMixin],
        data() {
            return {
                searchForm: {
                    categoryCode: '',
                    categoryName: '',
                    categoryStatus: ''
                },
                addOrUpdateVisible: false,
                tigger: false,
            }
        },
        components: {
            AddOrUpdate
        },
        mounted() {
            this.getDataTable()
        },
        methods: {
            // 获取数据列表
            getDataTable() {
                this.openPageTableLoading();
                let query = {
                    page: this.page.page,
                    limit: this.page.limit,
                    categoryStatus: this.searchForm.categoryStatus,
                    categoryCode: this.searchForm.categoryCode,
                    categoryName: this.searchForm.categoryName
                };
                goodcategoryService.getGoodCategoryTree(query).then(data => {
                    this.tableData = data
                }).catch((res) => {
                    this.closePageTableLoading();
                });
            },
            // 新增 / 修改
            addOrUpdate(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            // 管理事件
            managerHandle(id) {

            },
            // 删除/批量删除
            deleteHandle(id) {
                var ids = id ? [id] : this.multipleSelection.map(item => {
                    return item.pkCategory
                })
                if (ids.length > 0) {
                    this.$confirm("确认删除[" + ids.length + "]条数据？", "确认信息", {
                        distinguishCancelAndClose: true,
                        confirmButtonText: "删除",
                        cancelButtonText: "取消"
                    }).then(() => {
                        goodcategoryService.delBatchGoodCategory(ids).then(() => {
                            this.$message.success("删除成功！");
                            this.getDataTable();
                        });
                    });
                } else {
                    this.$message.error("请选择数据！")
                }
            },
            // 展开/合并
            tableExpandTigger() {
                this.tigger = !this.tigger
                let rows = this.tableData
                this.dg(rows, this.tigger)
            },
            dg(rows, status) {
                let that = this
                rows.forEach((item, index) => {
                    if (item.children) {
                        that.dg(item.children, status)
                        this.$refs.dataTable.$refs.elTable.toggleRowExpansion(item, status)
                    }
                })
            },
        }
    }
</script>